<?php

use JPush\Client as JPush;
class JPusher
{
    private $client;

    function __construct($appKey, $masterSecret)
    {
        $config = C('jpush');
        $appKey = $appKey ?$appKey: $config['app_key'];
        $masterSecret = $masterSecret ?$masterSecret: $config['master_secret'];
        $this->client = new JPush($appKey, $masterSecret);
    }

    public function push($title,$content,$extras = null)
    {
        try {
            $ret = $this->client->push()
                ->setPlatform('all')
                 ->addAllAudience()
//                ->addAlias($to)
//                ->addTag(array('贴身兵王','总裁大哥独宠妻'))
                  ->iosNotification($title,['sound'=>'sound','badge'=>'+1','extras'=>$extras])
                  ->addAndroidNotification($content, $title, 1, $extras)
//                ->addIosNotification($content, null, $title, true, null, $extras)
//                ->addIosNotification("有新书推荐了哦！", 'iOS sound', 'fail', true, 'iOS category', $extras)
//                 ->setNotificationAlert($content)
                  ->setOptions(null, null, null, true)
                // 使用测试环境
//                 ->setOptions(null, null, null, false)
                ->send();
            return $ret;
        } catch (\APIRequestException $e) {
            // dump($e);
        }
    }
}
